package com.itheima.controller;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServicelmpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j   //声明日志
@RestController
public class DeptController {
    //private static Logger log= LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    private EmpMapper empMapper;
    //抽取类


    //@RequestMapping(value = "/depts" ,method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList= deptService.list();
        return Result.success(deptList);
    }

    @Transactional(rollbackFor = Exception.class)//spring事物管理，所有异常都会回滚
    //删除部门
    @DeleteMapping("/depts/{id}")
    public void delete(@PathVariable Integer id ) throws Exception {
        log.info("根据id删除部门：{}", id);
        //调用service
        deptService.delect(id);

        // throw new Exception("出错了");

        empMapper.deleteByDeptId(id);
    }


    @PostMapping("/depts")
    //添加部门@RequestBody声明实体类
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }
}
